package com.hava.homework.service;

import com.hava.homework.entity.UserBalance;
import com.hava.homework.repository.UserBalanceDaoInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yanfa on 2016/10/27.
 */
@Service
public class TransferBalanceService {

    UserBalanceDaoInterface userBalanceDao;

    public void setUserBalanceDao(UserBalanceDaoInterface userBalanceDao) {
        this.userBalanceDao = userBalanceDao;
    }

    public List<UserBalance> findAll(){
        return this.userBalanceDao.findAll();
    }

    // srcUserId及targetUserId为转帐用户标识
    @Transactional
    public void transferMoney(Long srcUserId, Long targetUserId, double count){
        System.out.println("Class TransferBalanceService Method transferMoney");
        this.userBalanceDao.subMoney(srcUserId,count);
        throwException();
        this.userBalanceDao.addMoney(targetUserId,count);
    }

    private void throwException() {
        throw new RuntimeException("Throw Exception");
    }
}
